<?php
    include 'head.php';


		$cate = $_GET['cid'];
		$style = $_GET['st'];
		$keyword = $_GET['keyword'];

        //获取路径信息，用来做导航条
        $sql = "select concat(path,id) bpath from ".PRE."category where id='{$cate}'";
        $pathlist = mysql_fetch_assoc(mysql_query($sql));
        $patharr = explode(',',$pathlist['bpath']);

		
		//如果有搜索关键字，执行名称匹配
		if($keyword == null){
			if($cate != null) $search[] = "(cate_id in (select id from ".PRE."category where path like '%,{$cate},%') || cate_id = '{$cate}')";
			if($style != null) $search[] = "style='{$style}'";
		}else{
			$search[]="name like '%$keyword%'";
		}
		if($search != null ) $where = 'and '.implode(' and ',$search);

		//如果有搜索关键字，不查询类别banner图
		if($keyword == null){				//取出所需数据，banner图和商品数组
			$sql = "select name,cate_id,style from ".PRE."image where size='1' $where limit 1";
			$banner = mysql_fetch_assoc(mysql_query($sql));
			//$banner 是一个一维数组，里面三个元素，name cate_id ，style
		}
		
		//处理分页数据
		//获取总记录数
			$where1 = ltrim($where,'and');
			$sql = "select count(id) num from ". PRE ."goods where {$where1}" ;
			$result = mysql_query($sql);
			
			if($result && mysql_affected_rows() > 0){
				$row = mysql_fetch_assoc($result);
			}

			$total = $row['num'];       //总记录数
			//每页显示数
			$num = 5;

			//总页数
			$amount = ceil($total / $num);

			//页码
			$page = empty($_GET['p'])? 1 : (int)$_GET['p'];

			if($page < 1){
				$page = 1;
			}

			if($page > $amount){
				$page = $amount;
			}

			//偏移量
			$offset = ($page - 1) * $num;  
		
		//取出商品数据
		$sql = "select id,cate_id,name,price,image from ".PRE."goods where is_up='1' $where order by addtime desc limit $offset,$num";
		$result = mysql_query($sql);
		$goodslist = array();
		while($row = @mysql_fetch_assoc($result)){
			$goodslist[] = $row;
		}
		@mysql_free_result($result);
		//$goodslist 是一个二维数组，遍历取出里面的商品信息，包含id,cate_id,name,price,image字段
		
		
		//制作分页按钮
		$start = ($page - 3) <= 1 ? 1 : $page - 3;

		$end = ($page + 3) >= $amount ? $amount : $page + 3 ;

		for($i = $start; $i <= $end; $i++){
			if($i == $page){
				$str .= '<li><a style="color:#E50065;font-weight:bold;" href="secondary.php?cid='.$cate.'&keyword='.$keyword.'&p='. $i .'" >['. $i .']</a></li>';
				continue;
			}
				$str .= '<li><a href="secondary.php?cid='.$cate.'&keyword='.$keyword.'&p='. $i .'">['. $i .']</a><li>';

		}


    $nav = <<<tag
        <li><a href="secondary.php?cid={$cate}&keyword=$keyword&p=1">首页</a></li>
        $str
        <li><a href="secondary.php?cid={$cate}&keyword=$keyword&p=$amount">尾页</a></li>
tag;


?>

<!-- 这里编辑首页的主体内容 -->
	<div id="secondary" class="w">
        <!-- 导航信息 -->
        <div class="sec_nav mt20">
            <?php if(!empty($pathlist)):?>
            <?php foreach($patharr as $val):?>
            <?php
                $sql = "select name from ".PRE."category where id='{$val}'";
                $pathname = mysql_fetch_assoc(mysql_query($sql));
                if($val == 0) echo '<a href="index.php"><span>首页</span></a>';
                else echo '<a href="secondary.php?cid='.$val.'"><span>'.$pathname['name'].'</span></a>';
            ?>
            <span>&gt;&gt;</span>
            <?php endforeach;?>
            <?php else:?>
                <span>搜索结果<span>
                <span>&gt;&gt;</span>
            <?php endif;?>
        </div>
        <!-- 分类信息 -->
        <?php 
            //查询子分类信息
            $sql = "select id,name from ".PRE."category where pid='0'";
            $result = mysql_query($sql);
            while($row = mysql_fetch_assoc($result)){$catemain[] = $row;}
        ?>
		<div class="class mt10">
            
            <table cellspacing="0">
                <?php foreach($catemain as $caterow):?>
                <tr height="25">
                    <th width="60"><a href="secondary.php?cid=<?php echo $caterow['id']?>"><?php echo $caterow['name']?></a></th>
                    <?php
                        $sql = "select id,name from ".PRE."category where pid=".$caterow['id'];
                        $result = mysql_query($sql);
                        $catenav = array();
                        while($row = mysql_fetch_assoc($result)){$catenav[]=$row;}
                    ?>
                    <?php foreach($catenav as $val):?>
                    <td width="80"><a href="secondary.php?cid=<?php echo $val['id']?>"><?php echo $val['name']?></a></td>
                    <?php endforeach;?>
                </tr>
                <?php endforeach;?>
            </table>
		</div>
		
			<div class="good_list mt20">
	
				
				<?php if(!empty($goodslist)):?> 
				<div class="title">
				<a href="./secondary.php?cid=<?php echo $banner['cate_id']?>&st=<?php echo $banner['style']?>" class="fl fontyh"><span><?php 
					//如果有搜索关键字，title显示搜索的关键字
					if($keyword == null){
						$sql = "select name from ".PRE."style where id='{$style}'";
						$list = mysql_fetch_assoc(mysql_query($sql));
						echo $list['name'];	
						echo '-';
						$sql = "select name from ".PRE."category where id='{$cate}'";
						$list = mysql_fetch_assoc(mysql_query($sql));
						echo $list['name'];
					}else{
						echo '搜索关键字　'.$keyword;
					}
				?><span></a>
					<a href="./secondary.php?cid=<?php echo $banner['cate_id']?>&st=<?php echo $banner['style']?>" class="fr"> 更多 >></a>
					<div class="clear"></div>
				</div>
				<div>
					<a href="./secondary.php?cid=<?php echo $banner['cate_id']?>&st=<?php echo $banner['style']?>"><img src="./upload/<?php echo $banner['name'] ?>" alt="" /></a>
				</div>
				<div class="sub_list mt20">
					<ul class="secondary">
						<?php foreach($goodslist as $val): ?>
						<li class="fl">
							<div class="pic">
								<a href="./detail.php?gid=<?php echo $val['id']?>&cid=<?php echo $val['cate_id']?>"><img src="./upload/<?php echo $val['image']?>" width="235" alt="" /></a>
							</div>
							<div class="clear"></div>
							<div class="name_price">
								<a href="./detail.php?gid=<?php echo $val['id']?>&cid=<?php echo $val['cate_id']?>"><?php echo $val['name'] ?></a><br />
								<span class="newprice fontyh">￥<?php echo $val['price']?></span>
								<span>市场价：￥<?php echo ceil($val['price']/0.7) ?></span>
							</div>
						</li>
						<?php endforeach; ?>
						<div class="clear"></div>
					</ul>
				</div>
			<?php else:?>
				<div class="w sorry">很抱歉！没有查询到相关数据</div>
			<?php endif; ?>
			<?php if($total > $num):?>
			<div class="searchnav">
				<div class="tips"><?php $start = $offset + 1>0? $offset+1: 0;$end = $offset + $num >= $total? $total : $offset + $num; echo "Showing $start to $end of $total entries";?></div>
				<ul class="user_nav">
					<?php echo $nav?>
				</ul>
			</div>
			<?php endif;?>


			<!-- 这里是推荐板块 -->
			<?php
					//查询推荐板块
					$sql = "select cate_id,style from ".PRE."homepage where recommend='1'";
					$result = mysql_query($sql); 
					while($row = mysql_fetch_assoc($result)){
						$showlist[] = $row;
					}
					mysql_free_result($result);
				?>
			<?php if(!empty($showlist)):?>
			<?php foreach($showlist as $show):?>
			<div class="good_list mt20">
				<?php 
					$cate = $show['cate_id'];
					$style = $show['style'];
					//取出所需数据，banner图和商品数组
					$sql = "select name,cate_id,style from ".PRE."image where size='1' and cate_id='{$cate}' and style='{$style}'";
					$banner = mysql_fetch_assoc(mysql_query($sql));
					//$banner 是一个一维数组，里面三个元素，name cate_id ，style

					//取出商品数据
					$sql = "select id,cate_id,name,price,image,addtime from ".PRE."goods where (cate_id in(select id from ".PRE."category where path like '%,{$cate},%') || cate_id = '{$cate}') and style='{$style}' and is_up='1' order by addtime desc limit 5";
					$result = mysql_query($sql);
					$goodslist = array();
					while($row = mysql_fetch_assoc($result)){
						$goodslist[] = $row;
					}
					mysql_free_result($result);
					//$goodslist 是一个二维数组，遍历取出里面的商品信息，包含id,cate_id,name,price,image字段	
				?>	
				
				<div class="title">
				<a href="./secondary.php?cid=<?php echo $banner['cate_id']?>&st=<?php echo $banner['style']?>" class="fl fontyh"><span>推荐板块<span></a>
					<a href="./secondary.php?cid=<?php echo $banner['cate_id']?>&st=<?php echo $banner['style']?>" class="fr"> 更多 >></a>
					<div class="clear"></div>
				</div>
				<div>
					<a href="./secondary.php?cid=<?php echo $banner['cate_id']?>&st=<?php echo $banner['style']?>"><img src="./upload/<?php echo $banner['name'] ?>" alt="" /></a>
				</div>
				<div class="sub_list mt20">
					<ul>
						<?php foreach($goodslist as $val): ?>
						<li class="fl">
							<div class="pic">
								<a href="./detail.php?gid=<?php echo $val['id']?>&cid=<?php echo $val['cate_id']?>"><img src="./upload/<?php echo $val['image']?>" width="235" alt="" /></a>
							</div>
							<div class="clear"></div>
							<div class="name_price">
								<a href="./detail.php?gid=<?php echo $val['id']?>&cid=<?php echo $val['cate_id']?>"><?php echo $val['name'] ?></a><br />
								<span class="newprice fontyh">￥<?php echo $val['price']?></span>
								<span>市场价：￥<?php echo ceil($val['price']/0.7) ?></span>
							</div>
						</li>
						<?php endforeach; ?>
						<div class="clear"></div>
					</ul>
				</div>
			</div>
		<?php endforeach;?>
		<?php endif;?>
		</div>
	</div>
<?php
    include 'footer.php';
?>
